package contegor.scire.structure;

import contegor.scire.core.Connectable;
import contegor.scire.core.queries.Condition;

/**
 * Follows Wrapper pattern. Filters the events that receives the wrapped {@link ContextListener} based on
 * a given {@link Condition}.
 * 
 * @author  Héctor Cirbián Pousa
 */
public class ContextListenerFilter implements ContextListener {

  private ContextListener wrappedListener;
  private Condition<Connectable> condition;

  public ContextListenerFilter(ContextListener wrappedListener, Condition<Connectable> condition) {
    super();
    this.wrappedListener = wrappedListener;
    this.condition = condition;
  }

  @Override
  public void added(Connectable element) {
    if (condition.eval(element))
      this.wrappedListener.added(element);
  }

  @Override
  public void deleted(Connectable element) {
    if (condition.eval(element))
      this.wrappedListener.deleted(element);
  }

  @Override
  public void updated(Connectable element) {
    if (condition.eval(element))
      this.wrappedListener.updated(element);    
  }

}